package com.zzl.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class PathLoggingFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取请求对象
        ServerHttpRequest request = exchange.getRequest();

        // 获取完整URL
        String url = request.getURI().toString();

        // 打印日志或进行其他处理
        log.info("请求 URL: {}", url);
        // 继续执行下一个过滤器
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        // 设置过滤器执行顺序，数值越小优先级越高
        return 0;
    }
}
